<template>
  <div>
    <a-table
      rowKey="id"
      size="middle"
      :columns="columns"
      :dataSource="gDataSource"
      :pagination="gPagination"
      @change="gHandleTableChange"
    >
      >
      <span
        slot="sortCol"
        slot-scope="text, record, index"
      >
        {{
          (gPagination.current - 1) * gPagination.pageSize + parseInt(index) + 1
        }}
      </span>

        <span
          slot="action"
          slot-scope="text, record"
        >
          <template v-if="!record.notifyEnable">
            <a-popconfirm
              title="确定开启吗?"
              @confirm="() => handleStatusChange(record)"
            >
              <a-button type="primary" icon="check-circle" size="small">开启邮箱通知</a-button>
            </a-popconfirm>
          </template>
          <template v-if="record.notifyEnable">
            <a-popconfirm
              title="确定关闭吗?"
              @confirm="() => handleStatusChange(record)"
            >
              <a-button type="danger" icon="close-circle" size="small">关闭邮箱通知</a-button>
            </a-popconfirm>
          </template>
        </span>
    </a-table>
  </div>
</template>

<script>
  import {
    GwListMixin
  } from '@/mixins/GwListMixin'
  import {
    gPostAction
  } from '@/api/manage'

  export default {
    name: 'Notice',
    mixins: [GwListMixin],
    props: {
      modelType: {
        type: String,
        default: ''
      },
    },
    data() {
      return {
        // 表头
        columns: [{
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 50,
          scopedSlots: { customRender: "sortCol" },
        },
        {
          title: '策略名称',
          dataIndex: 'policyName',
        },
        {
          title: '添加时间',
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: {
            customRender: 'action'
          },
        }, ],
        url: {
          list: '/policy/page',
          update: '/policy/update1',
        },
      };
    },
    mounted() {
      this.loadData()
    },
    methods: {
      loadData() {
        if (this.modelType === 'f010104') {
          this.gQueryParam.resourceType = "";
          this.gQueryParam.title = "精确管控与供电制冷联动-基础资源细粒度感知-诊断告警-告警通知方式设置";
          this.gQueryParam.content = "查询告警通知方式列表";
        } else if (this.modelType === 'f010305') {
          this.gQueryParam.resourceType = "environment";
          this.gQueryParam.title = "精确管控与供电制冷联动-供电制冷最优模型-供电制冷设备告警管理-告警通知方式设置";
          this.gQueryParam.content = "查询告警通知方式列表"
        }
        gPostAction(this.url.list, this.gQueryParam).then(
          (res) => {
            const data = res.data;
            if (data && data.list) {
              this.gDataSource = data.list;
              this.gPagination.total = data.totalCount;
            }
          })
      },
      handleStatusChange(data) {
        const params = {
          policyId: data.id,
          notifyEnable: !data.notifyEnable,
          title: "精确管控与供电制冷联动-供电制冷最优模型-供电制冷设备告警管理-告警通知方式设置"
        };
        gPostAction(this.url.update, params)
          .then((res) => {
            if (res.success) {
              this.$message.success("操作成功");
            }
          })
          .finally(() => {
            this.loadData()
          })
      },
    },
  }
</script>
